﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace Watson.TemporaryFilesDeleter
{
    /// <summary>
    /// Abstract base class for filtering a collection of files against a
    /// criterion.
    /// </summary>
    public abstract class FileFilter : IFileFilter
    {
        #region Protected Methods

        /// <summary>
        /// When implemented in a derived class, determines whether to include
        /// a specific file from the input collection in the output collection.
        /// </summary>
        /// <param name="file">
        /// The file for which to determine its inclusion in the output
        /// collection.
        /// </param>
        /// <returns>
        /// System.Bool
        /// <para>
        /// True if the file passes the criterion and should be included in the
        /// output collection; else, false.
        /// </para>
        /// </returns>
        /// <exception cref="System.ArgumentNullException">
        /// <paramref name="file"/> was null.
        /// </exception>
        protected abstract bool IncludeFile(FileInfo file);

        #endregion

        #region Public Methods

        /// <summary>
        /// Filters a collection of files against a criterion.
        /// </summary>
        /// <param name="files">
        /// A collection of files to filter.
        /// </param>
        /// <returns>
        /// System.Collections.Generic.ICollection&gt;System.IO.FileInfo&lt;
        /// <para>
        /// A filtered collection of files. Files that did not satisfy the
        /// criterion are omitted from the input collection.
        /// </para>
        /// </returns>
        /// <exception cref="System.ArgumentNullException">
        /// <paramref name="files"/> was null.
        /// </exception>
        public ICollection<FileInfo> ApplyFilter(ICollection<FileInfo> files)
        {
            List<FileInfo> filteredFiles = new List<FileInfo>();

            foreach (var file in files)
            {
                if (IncludeFile(file))
                {
                    filteredFiles.Add(file);
                }
            }

            return filteredFiles;
        }

        #endregion

        #region Protected Constructors

        /// <summary>
        /// Initialises a new instance of the FileFilter class.
        /// </summary>
        protected FileFilter()
        { }

        #endregion
    }
}
